package xj.model.uml;

import static xj.model.uml.UMLModelConstants.ROLE_USE_CASE;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import xj.meta.ModelElement;

public class UMLSystemBoundary extends NamedElement {

  public UMLSystemBoundary() {
  }

  public UMLSystemBoundary(String name) {
    super(name);
  }

  public void addUseCase(UseCase u) {
    if (u != null) {
      usecases.add(u);
      addOwnedElement(u, ROLE_USE_CASE);
    }
  }

  public void removeUseCase(UseCase u) {
    if (u != null) {
      usecases.remove(u);
      removeOwnedElement(u);
    }
  }

  public Set<UseCase> getUseCases() {
    return usecases;
  }

  public void addUMLAssociation(UMLAssociation assoc) {
    if (assoc != null) {
      associations.add(assoc);
      addOwnedElement(assoc);
    }
  }

  public void removeUMLAssociation(UMLAssociation assoc) {
    if (assoc != null) {
      associations.remove(assoc);
      removeOwnedElement(assoc);
    }
  }

  public Set<UMLAssociation> getAssociations() {
    return associations;
  }

  @Override
  public void deconstruct() {
    super.deconstruct();
    usecases.clear();
    associations.clear();
  }

  public void add(ModelElement parent, ModelElement e, Object arg) {
    if (e != null) {
      if (e instanceof UseCase) {
	addUseCase((UseCase) e);
      } else if (e instanceof UMLAssociation) {
	addUMLAssociation((UMLAssociation) e);
      } else {
	// super.add(parent, e, arg);
      }
    }
  }

  @Override
  public String toString() {
    return "UMLSystemBoundary: name=" + name;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    UMLSystemBoundary sys = (UMLSystemBoundary) super.clone();

    sys.usecases = new HashSet<UseCase>();
    Iterator<UseCase> iter1 = usecases.iterator();
    while (iter1.hasNext()) {
      UseCase u = iter1.next();
      sys.addUseCase((UseCase) u.clone());
    }

    sys.associations = new HashSet<UMLAssociation>();
    Iterator<UMLAssociation> iter2 = associations.iterator();
    while (iter2.hasNext()) {
      UMLAssociation a = iter2.next();
      sys.addUMLAssociation((UMLAssociation) a.clone());
    }

    return sys;
  }

  protected Set<UseCase> usecases = new HashSet<UseCase>();

  protected Set<UMLAssociation> associations = new HashSet<UMLAssociation>();

}
